import Vue from 'vue';
import Vuex from 'vuex';
import { HttpManager } from '@/api';
import { Notify } from 'vant';
import router from '@/router'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {//全局对象
    userInfo: {},
  },
  mutations: {//同步修改state值
    setUserInfo(state, userInfo) {
      Object.assign(state.userInfo, {
        ...userInfo,
      })
      console.log("--userInfo--", state.userInfo);
    },
    clearUserInfo(state) {
      state.userInfo = {};
    }
  },
  actions: {//异步修改state值
    getUserInfo({ commit }, values) {
      HttpManager.LoginApi(values).then((res) => {
        if (res.code === 0) {
          sessionStorage.setItem("userid", res.data.uid);
          console.log("--LoginApi--", res);
          if(res.data.temp1 === "住房维护人员"){
            Notify({ type: "success", message: res.msg });
            router.push("temp");
          }
          else {
            commit('setUserInfo', { iscer: res.data.iscer });
            HttpManager.users_statecer(sessionStorage.getItem("userid")).then((cerRes) => {
              if (cerRes.code === 0) {
                Notify({ type: "success", message: "欢迎回来" });
                router.push("home");
                console.log(cerRes.data);
              }
              else if (cerRes.code === 13009) {//未认证
                Notify({ type: "danger", message: cerRes.msg });
                router.push("certificate");
              }
              else if (cerRes.code === 13010) {//认证待审核
                Notify({ type: "warning", message: cerRes.msg });
                router.push("home");
              }
              else if (cerRes.code === 13011) {//审核未通过
                Notify({ type: "danger", message: cerRes.msg });
                router.push("certificate");
              }
              else {
                Notify({ type: "danger", message: cerRes.msg });
              }
              commit('setUserInfo', cerRes.data);
            });
          }
        } else {
          Notify({ type: "danger", message: res.msg });
        }
      });
    },
  },
})
